//图表分析
var emotionalAnalysis;
require(['grid', 'topBar', 'date', 'highcharts'], function () {
    //提交到后台参数
    var postData;
    $('#top').topBar({
        left: [
            {
                type: 'filter',
                search: function () {
                    search();
                },
                item: [
                    {
                        text: '时间快捷',
                        name: 'reportType',
                        targetId: 'beginTime',
                        type: 'reportDateSelect'
                    }, {
                        text: '查询时间',
                        id: 'beginTime',
                        name: 'beginTime',
                        type: 'text',
                        width: 250
                    }
                ]
            }
        ],
        right: [{
            text: '导出',
            type: 'export-btn',
            authority: '',
            item: [{
                text: 'Excel导出',
                click: function () {
                    exportFile('Excel');
                }
            }, {
                text: 'PDF导出',
                click: function () {
                    exportFile('Pdf');
                }
            }]
        }]
    });
    function exportFile(suffix) {
        if ($('#grid').getGridParam("reccount") < 1) {
            $.messager.alert("提示", "请先查询数据。", "info");
            return;
        }
        if (getPostData()) {
            var url = "/driverEmotional/exportDriverEmotional" + suffix + ".htm";
            $.exportFile(url, postData);
        }
    }

    $('#grid').parent().height($(window).height() - 153);

    //图表分析
    emotionalAnalysis = function (obj) {
        var params = [];
        var val = $('#top').parent().getValues();
        params.push({
            name: "drvId",
            value: obj.drvId
        }, {
            name: "score",
            value: obj.score||0
        }, {
            name: "organName",
            value: obj.organName
        }, {
            name: "lineName",
            value: obj.lineName
        }, {
            name: "drvName",
            value: obj.drvName
        }, {
            name: "beginTime",
            value: val.beginTime
        }, {
            name: "lineId",
            value: obj.lineId
        });
        $.ajaxExt({
            url: "/driverEmotional/searchDrvEmotionalInfo.htm",
            data: params
        }).done(function (result) {
            $("#emoDrvName").html(obj.drvName);
            if (result[1] == "优秀") {
                $("#lv").html("<font style = 'color:green;'>" + result[1] + "</font>");
            } else if (result[1] == "良好") {
                $("#lv").html("<font style = 'color:orange;'>" + result[1] + "</font>");
            } else {
                $("#lv").html("<font style = 'color:red;'>" + result[1] + "</font>");
            }
            $("#content").html("<b>评语:</b>" + result[2]);
            $("#tempCount").html(result[3]);
            $("#tempPercentage").html(result[4]);
            $("#emotionalHighcharts").parent().show();
            var tmp = [];
            for (var a = 0; a < 31; a++) {
                for (var b = 0; b < 12; b++) {
                    var value = '';
                    tmp.push([a, b, value]);
                }
            }
            if (result[0] != null) {
                $.each(result[0], function (index, item) {
                    var dateSplitArr = item.subtime.split('-'),
                        month = parseInt(dateSplitArr[1]),
                        day = parseInt(dateSplitArr[2]);
                    tmp[(month - 1) + (day - 1) * 12][2] = item.counttype;
                });
                if(!$.isEmptyObject(tmp)){
                    $("#emotionalHighcharts").show();
                    $("#emotionalHighcharts").highcharts({
                        chart: {
                            type: 'heatmap',
                            marginTop: 40
                        },
                        title: {
                            text: ''
                        },
                        xAxis: {
                            categories: ["01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31"],
                            labels: {
                                align: 'center',
                                y: 50
                            }
                        },
                        yAxis: {
                            categories: ["01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"],
                            title: null
                        },
                        plotOptions: {
                            heatmap: {
                                enableMouseTracking: true,
                                nullColor: "#2BC416"

                            }
                        },
                        credits: {
                            enabled: false
                        },
                        colorAxis: {
                            min: 0,
                            max: 1,
                            minColor: '#2BC416',
                            maxColor: '#FA0808'
                        },
                        legend: {
                            enabled: false
                        },
                        tooltip: {
                            enabled: false
                        },
                        series: [
                            {
                                name: '',
                                borderWidth: 1,
                                borderColor: '#ffffff',
                                data: tmp,
                                dataLabels: {
                                    enabled: true,
                                    color: 'black',
                                    style: {
                                        fontWeight: "normal",
                                        textShadow: 'none',
                                        HcTextStroke: null
                                    }
                                }
                            }
                        ]
                    });
                }
            }
        });
    }

    $('#grid').grid({
        multiselect: false,
        url: "/driverEmotional/searchDriverEmotional.htm",
        colModel: [{name: "lineId", label: "lineId", hidden: true},
            {name: "drvId", label: "drvId", hidden: true},
            {name: "rcrdTime", label: "发生时间", hidden: true},
            {name: "allrank", label: "排名", align: "right", width: 40},
            {name: "score", label: "得分", align: "right", width: 40},
            {name: "drvName", label: "驾驶员", width: 80},
            {name: "lineName", label: "营运线路", width: 80},
            {name: "organName", label: "所属机构", width: 80},
            {name: "totalOprtDura", label: "运行时长", align: "right", width: 80},
            {name: "totalMile", label: "运行里程(KM)", align: "right", formatter: "currency", width: 120},
            {name: "sumabnormal", label: "异常次数", align: "right", width: 80},
            {name: "cntIeta", label: "惯性异常", align: "right", width: 80},
            {name: "cntImpati", label: "心情急躁", align: "right", width: 80},
            {name: "cntOvertake", label: "超车未果", align: "right", width: 80}],
        onSelectRow: function (ids) {
            var data = $('#grid').getRowData(ids);
            emotionalAnalysis(data);
        }
    });

    function search() {
        if (getPostData()) {
            $('#grid').grid('setGridParam', {
                page: 1,
                datatype: "json",
                postData: postData
            }).trigger("reloadGrid");
        }
    }

    function getPostData() {
        postData = $('#top').parent().getValues();
        if(!postData.beginTime){
            $.messager.alert("提示", "请先选择查询时间。", "info");
            return false;
        }
        var nodes = commonFun.getLeftTreeCheckedNodes();
        if (nodes.LINE) {
            postData.orgIds = nodes.LINE;
            postData.type = "LINE";
            return true;
        }
        $.messager.alert("提示", "至少请选择一条线路。", "info");
        return false;
    }

})
